package server

import (
	"context"
	"errors"
	__ "user-srv/basic/proto"
	"user-srv/handler/models"
)

type Server struct {
	__.UnimplementedUserServer
}

// 用户注册
func (s *Server) RegisterUser(_ context.Context, in *__.RegisterUserReq) (*__.RegisterUserResp, error) {
	var user models.User
	if err := user.GetUserInfo(in.Mobile); err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	user = models.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	return &__.RegisterUserResp{
		UserId: int64(user.ID),
	}, nil
}

// 用户登录
func (s *Server) LoginUser(_ context.Context, in *__.LoginUserReq) (*__.LoginUserResp, error) {
	var user models.User
	if err := user.GetUserInfo(in.Mobile); err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID != 0 {
		return nil, errors.New("用户已存在")
	}
	if user.Password != in.Password {
		return nil, errors.New("密码错误")
	}
	return &__.LoginUserResp{
		UserId: int64(user.ID),
	}, nil
}
